package cn.com.lyb.flink.sql;

import org.apache.flink.connector.jdbc.catalog.JdbcCatalog;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.TableResult;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.Catalog;

public class SqlDemo {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        tableEnv.registerCatalog("mysql", new JdbcCatalog("mysqlcatalog","lyb", "root",
                "123456","jdbc:mysql://172.32.1.6:3367"));
        tableEnv.useCatalog("mysql");
        tableEnv.executeSql("CREATE TABLE `se_serve`  (\n" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',\n" +
                "  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称',\n" +
                "  `dir_id` bigint(20) NOT NULL COMMENT '目录id',\n" +
                "  `lifespan` int(11) NULL DEFAULT NULL COMMENT '有效期（单位:天数，发布会使用此时间）',\n" +
                "  `lifespan_subsidiary` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '有效期附属多选择,1 临期通知，2临期续订',\n" +
                "  `description` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '描述',\n" +
                "  `type` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '类型：1.数据库表，2.资产，3.文件，4.代理url 5.主数据',\n" +
                "  `authentication` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '安全校验：1.身份校验 2公开',\n" +
                "  `watermark` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '水印设置：1 开启 0 关闭',\n" +
                "  `secret_id` bigint(20) NULL DEFAULT NULL COMMENT '密钥id',\n" +
                "  `status` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '状态：1.待发布，2.上架，3，到期，4.下架',\n" +
                "  `create_user_id` bigint(20) NOT NULL COMMENT '创建者id',\n" +
                "  `create_time` datetime NOT NULL COMMENT '创建时间',\n" +
                "  `update_time` datetime NULL DEFAULT NULL COMMENT '修改时间',\n" +
                "  `publish_time` datetime NULL DEFAULT NULL COMMENT '发布时间',\n" +
                "  `revoke_time` datetime NULL DEFAULT NULL COMMENT '撤销时间',\n" +
                "  `model_id` bigint(20) NOT NULL COMMENT '流程id',\n" +
                "  `model_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '流程名称',\n" +
                "  `model_type` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '流程类型 0 常规流程 1 资产服务',\n" +
                "  `model_reviewed_id` bigint(20) NULL DEFAULT NULL COMMENT '流程责任人',\n" +
                "  `collects` int(11) NULL DEFAULT 0 COMMENT '收藏数',\n" +
                "  `asset_id` bigint(20) NULL DEFAULT NULL COMMENT '资产id',\n" +
                "  `del` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '删除标识：1：已删除 0：未删除',\n" +
                "  PRIMARY KEY (`id`) USING BTREE\n" +
                ") ENGINE = InnoDB AUTO_INCREMENT = 22 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '服务信息-主体表' ROW_FORMAT = Dynamic;\n");
        Table table = tableEnv.sqlQuery("select * from lyb.ws");
        // 把table 注册为表名
        TableResult execute = table.execute();
        execute.print();
    }
}
